<?php
require_once "../load.php";
error_reporting(0);
$action = $_POST["action"];
unset($_POST["action"]);
if (get_magic_quotes_gpc()) {
    $_POST = Owl_Format::stripslashes_deep($_POST);
}
if (!defined(INT_JOUR_PAPER))
    define('INT_JOUR_PAPER', 'International Journal Paper');
if (!defined(NAT_JOUR_PAPER))
    define('NAT_JOUR_PAPER', 'National Journal Paper');
if (!defined(INT_CONF_PAPER))
    define('INT_CONF_PAPER', 'International Conference Paper');
if (!defined(NAT_CONF_PAPER))
    define('NAT_CONF_PAPER', 'National Conference Paper');

switch ($action) {
    case 'add_product' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }

        $owl_product = new Owl_Product();
        $max_id = $owl_product->get_max_id();
        $data = array(
            "name" => "Product " . $max_id,
            "year" => trim($_POST["product_year"]),
            "description" => "Product description " . $max_id
        );
        
        if ($_POST["type"] == "paper") {
            switch ($_POST["product_type"]) {
                case 0 :
                    $data["type"] = INT_JOUR_PAPER;
                    break;
                case 1 :
                    $data["type"] = NAT_JOUR_PAPER;
                    break;
                case 2 :
                    $data["type"] = INT_CONF_PAPER;
                    break;
                case 3 :
                    $data["type"] = NAT_CONF_PAPER;
                    break;
                default :
                    $data["type"] = INT_JOUR_PAPER;
                    break;
            }

            //Upload the file
            $file_data = $_FILES['paper_file'];
            if ($file_data['error'] == 0 && is_uploaded_file($file_data['tmp_name'])) {
                $file_info = pathinfo($file_data['name']);
                $file_name = Owl_Date_Time::current_time('timestamp') . '.' . $file_info['extension'];
                $aps_path = PUBLICATION_PATH;
                $rel_path = UPLOAD_DIR . '/' . PUBLICATION_DIR . '/';

                if (!@move_uploaded_file($file_data['tmp_name'], $aps_path . "/" . $file_name)) {
                    $message = array(
                        "code" => 0,
                        "message" => "<div class=\"ajax-error\">" . sprintf(__("Cannot create file %s in folder %s"),$file_name, $rel_path) . "</div>"
                    );
                    $message = Owl_Functions::json_encode($message);
                    echo '<textarea>' . $message . '</textarea>';
                    die();
                }

                $data['link'] = $rel_path . $file_name;
            }
            elseif ($file_data['error'] == 4) {
                $data["link"] = "";
            }
            else {
                $message = array (
                    "code" => 0,
                    "message" => "<div class=\"ajax-error\">" . __("Error while uploading file, your data is not affected by this error.") . "<br />" . Owl_Functions::get_upload_error($file_data["error"]) . "</div>"
                );
                $message = Owl_Functions::json_encode($message);
                echo '<textarea>' . $message . '</textarea>';
                die();
            }
        }
        else {
            $data["type"] = "Project";
            $data["link"] = trim($_POST["product_link"]);
        }

        if ($owl_product->insert_products($data)) {
            //Language
            $name_lang["id"] = $data["name"];
            $name_lang[$_POST["lang"]] = trim(Owl_Format::esc_html($_POST["product_name"]));
            $description_lang["id"] = $data["description"];
            $description_lang[$_POST["lang"]] = trim(Owl_Format::esc_html($_POST["product_description"]));

            $owl_ulang = new Owl_Ulang();
            $owl_ulang->insert_ulang($name_lang);
            $owl_ulang->insert_ulang($description_lang);

            $message = array(
                "code" => 1,
                "message" => "<div class=\"ajax-success\">" . __("Insert product successful!") . "</div>"
            );
        }
        else {
            $message = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . __("Insert product error: ") . $owl_product->get_error() . "</div>"
            );
        }
        $message = Owl_Functions::json_encode($message);
        if ($_POST['type'] == 'paper') {
            echo '<textarea>' . $message . '</textarea>';
        }
        else {
            echo $message;
        }
        break;
    case 'edit_product' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }

        $old_name = trim($_POST["old_name"]);
        $data = array(
            "year" => trim($_POST["product_year"])
        );
        $owl_product = new Owl_Product();
        $old_product = $owl_product->get_product($old_name);

        if ($_POST["type"] == "paper") {
            switch ($_POST["product_type"]) {
                case 0 :
                    $data["type"] = INT_JOUR_PAPER;
                    break;
                case 1 :
                    $data["type"] = NAT_JOUR_PAPER;
                    break;
                case 2 :
                    $data["type"] = INT_CONF_PAPER;
                    break;
                case 3 :
                    $data["type"] = NAT_CONF_PAPER;
                    break;
                default :
                    $data["type"] = INT_JOUR_PAPER;
                    break;
            }

            //Upload the file
            $file_data = $_FILES['paper_file'];
            if ($file_data['error'] == 0 && is_uploaded_file($file_data['tmp_name'])) {
                $file_info = pathinfo($file_data['name']);
                $file_name = Owl_Date_Time::current_time('timestamp') . '.' . $file_info['extension'];
                $aps_path = PUBLICATION_PATH;
                $rel_path = UPLOAD_DIR . '/' . PUBLICATION_DIR . '/';
                if (!empty($old_product["link"])) {
                    if (!@unlink(APS_PATH . $old_product["link"])) {
                        $message = array(
                            "code" => 0,
                            "message" => "<div class=\"ajax-error\">" . sprintf(__("Cannot delete file %s"), $old_product["link"]) . "</div>"
                        );
                        $message = Owl_Functions::json_encode($message);
                        echo '<textarea>' . $message . '</textarea>';
                        die();
                    }
                }

                if (!@move_uploaded_file($file_data['tmp_name'], $aps_path . "/" . $file_name)) {
                    $message = array(
                        "code" => 0,
                        "message" => "<div class=\"ajax-error\">" . sprintf(__("Cannot create file %s in folder %s"),$file_name, $rel_path) . "</div>"
                    );
                    $message = Owl_Functions::json_encode($message);
                    echo '<textarea>' . $message . '</textarea>';
                    die();
                }

                $data['link'] = $rel_path . $file_name;
            }
            elseif ($file_data['error'] == 4) {
                $data["link"] = $old_product["link"];
            }
            else {
                $message = array (
                    "code" => 0,
                    "message" => "<div class=\"ajax-error\">" . __("Error while uploading file, your data is not affected by this error.") . "<br />" . Owl_Functions::get_upload_error($file_data["error"]) . "</div>"
                );
                $message = Owl_Functions::json_encode($message);
                echo '<textarea>' . $message . '</textarea>';
                die();
            }
        }
        else {
            $data["type"] = "Project";
            $data["link"] = trim($_POST["product_link"]);
        }
        
        if ($owl_product->update_products($old_name, $data)) {
            
            //Language
            $name_lang[$_POST["lang"]] = trim(Owl_Format::esc_html($_POST["product_name"]));
            $description_lang[$_POST["lang"]] = trim(Owl_Format::esc_html($_POST["product_description"]));

            $owl_ulang = new Owl_Ulang();
            $owl_ulang->update_ulang($old_product["name"], $name_lang);
            $owl_ulang->update_ulang($old_product["description"], $description_lang);
            
            $message = array(
                "code" => 1,
                "message" => "<div class=\"ajax-success\">" . __("Update product successful!") . "</div>"
            );
        }
        else {
            $message = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . __("Update product error: ") . $owl_product->get_error() . "</div>"
            );
        }
        $message = Owl_Functions::json_encode($message);
        if ($_POST['type'] == 'paper') {
            echo '<textarea>' . $message . '</textarea>';
        }
        else {
            echo $message;
        }
        break;
    case 'delete_product' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }

        $old_name = trim($_POST["old_name"]);
        $owl_product = new Owl_Product();
        $old_product = $owl_product->get_product($old_name);

        //Delete file
        if (!empty($old_product["link"]) && $old_product['type'] != 'Project') {
            if (!@unlink(APS_PATH . $old_product["link"])) {
                $message = array(
                    "code" => 0,
                    "message" => "<div class=\"ajax-error\">" . sprintf(__("Cannot delete file %s"), $old_product["link"]) . "</div>"
                );
                $message = Owl_Functions::json_encode($message);
                echo $message;
                die();
            }
        }

        if ($owl_product->delete_products($old_name)) {

            $owl_ulang = new Owl_Ulang();
            $owl_ulang->delete_ulang($old_product["name"]);
            $owl_ulang->delete_ulang($old_product["description"]);
            
            $message = array(
                "code" => 1,
                "message" => "<div class=\"ajax-success\">" . __("Delete info successful!") . "</div>"
            );
        }
        else {
            $message = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . __("Delete info error: ") . $owl_product->get_error() . "</div>"
            );
        }
        $message = Owl_Functions::json_encode($message);
        echo $message;
        break;
    case 'reload_paper_content' :
        $owl_product = new Owl_Product();
        $owl_product->print_papers_main();
        break;
    case 'reload_project_content' :
        $owl_product = new Owl_Product();
        $owl_product->print_projects_main();
        break;
    case 'input_lang_change' :
        $owl_product = new Owl_Product();
        $translated = $owl_product->get_product_with_lang($_POST["name"], $_POST["lang"]);
        if ($translated == false) {
            $data = array(
                "code" => 0
            );
        }
        else {
            $data = array(
                "code" => 1,
                "name" => $translated[0],
                "description" => $translated[1]
            );
        }
        $data = Owl_Functions::json_encode($data);
        echo $data;
        break;
    default:
        die();
}

die();